package com.tttare.haoyigou.search.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.HighlightOptions;
import org.springframework.data.solr.core.query.HighlightQuery;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleHighlightQuery;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;

import com.alibaba.dubbo.config.annotation.Service;
import com.tttare.haoyigou.pojo.TbItem;
import com.tttare.haoyigou.search.service.ItemSearchService;

@Service(timeout=10000)
public class ItemSearchServiceImpl implements ItemSearchService{

	@Autowired
	private SolrTemplate solrTemplate;
	
	@Override
	public Map search(Map searchMap) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Query query = new SimpleQuery("*:*");
		//SimpleQuery可以传递string类型的表达式参数,没有条件直接查询"*:*";如果有条件则可以用表达式或criteria对象
		//添加查询条件
		Criteria criteria=new Criteria("item_keywords").is(searchMap.get("keywords"));
		query.addCriteria(criteria);
		//查询
		ScoredPage<TbItem> scoredPage = solrTemplate.queryForPage(query, TbItem.class);
		resultMap.put("rows",scoredPage.getContent());
		return resultMap;
	}
	

	//查询列表
	public Map searchList(Map searchMap){
		Map<String,Object> resultMap=new HashMap<String, Object>();
		//开启高亮显示
		//高亮查询query对象
		HighlightQuery query = new SimpleHighlightQuery();
		//设置高亮显示的域字段
		HighlightOptions options=new HighlightOptions().addField("item_title");
		//高亮域前缀
		options.setSimplePrefix("<em style='color:red'>");
		//高亮域后缀
		options.setSimplePostfix("</em>");
		
		//1.关键字查询(即搜索框的输入:商家名,匹配,目录,标题的匹配搜索)
		Criteria criteria=new Criteria("item_keywords").is(searchMap.get("keywords"));
		query.addCriteria(criteria);
		
		//2.按商品分类
		
		//3.按品牌分类
		
		//4.按规格分类
		return null;
	}
}
